import {
  getSetting,
  chooseAddress,
  openSetting,
  showModal,
  showToast,
  requestPayment
} from "../../utils/asyncWx.js"
import regeneratorRuntime from "../../lib/runtime/runtime";
import {
  request
} from "../../request/index.js"

Page({
  data: {
    address: {},
    cart: [],
    totalPrice: 0,
    totalNum: 0
  },
  onShow() {
    // 获取缓存中的收货地址信息
    const address = wx.getStorageSync("address");
    // 获取缓存中的购物车数据
    let cart = wx.getStorageSync("cart") || [];
    // 过滤后的购物车数组
    cart = cart.filter(v => v.checked);
    this.setData({
      address
    })
    let totalPrice = 0;
    let totalNum = 0;
    cart.forEach(v => {
      totalPrice += v.num * v.goods_price;
      totalNum += v.num;
    });
    this.setData({
      cart,
      totalPrice,
      totalNum
    });
  },
  // 支付
  async handleOrderPay() {
    // 判断缓存中有没有token
    const token = wx.getStorageSync("token");
    const userInfo = wx.getStorageSync("userInfo");
    // 判断有没有登录
    if (!userInfo) {
      wx.navigateTo({
        url: '/pages/login/index',
      });
      return;
    }
    // 2 判断
    if (!token) {
      wx.navigateTo({
        url: '/pages/auth/index',
      });
      return;
    }
    wx.showToast({
      title: '假设支付成功，3秒后跳转到订单页面',
      icon: 'none',
      duration: 2000,
      mask: true,
    });
    setTimeout(() => {
      // 8 手动删除缓存中已经支付了的商品
      let newCart = wx.getStorageSync("cart");
      newCart = newCart.filter(v => !v.checked);
      wx.setStorageSync("cart", newCart);

      // 9 支付成功后跳转到订单页面
      wx.navigateTo({
        url: "/pages/order/index"
      })
    }, 3000);

    /* try {
      // 判断缓存中有没有token
      const token = wx.getStorageSync("token");
      // 2 判断
      if (!token) {
        wx.navigateTo({
          url: '/pages/auth/index',
        });
        return;
      }
      // console.log("已经存在token");
      // 3 创建订单
      // 准备请求头参数 假token
      const header = {
        Authorization: token
      };
      // 准备请求体参数
      const order_price = this.data.totalPrice;
      const consignee_addr = this.data.address.provinceName + this.data.address.cityName + this.data.address.countyName + this.data.address.detailInfo;
      const cart = this.data.cart;
      let goods = [];
      cart.forEach(v => goods.push({
        goods_id: v.goods_id,
        goods_number: v.num,
        goods_price: v.goods_price
      }))
      const orderParams = {
        order_price,
        consignee_addr,
        goods
      };
      // 4 准备发送请求 创建订单 获取订单编号
      // const {
      //   order_number
      // } = await request({
      //   url: "/my/orders/create",
      //   methods: "POST",
      //   data: orderParams,
      //   header
      // });
      // 假数据订单编号 HMDD20190802000000000422
      const order_number = 'HMDD20190802000000000422'
      // 5 发起预支付接口  
      // const {
      //   pay
      // } = await request({
      //   url: "my/orders/req_unifiedorder",
      //   methods: "POST",
      //   header,
      //   data: {
      //     order_number
      //   }
      // });
      // 假数据
      const pay = {
        "timeStamp": "1564730510",
        "nonceStr": "SReWbt3nEmpJo3tr",
        "package": "prepay_id=wx02152148991420a3b39a90811023326800",
        "signType": "MD5",
        "paySign": "3A6943C3B865FA2B2C825CDCB33C5304"
      }
      // 6 发起微信支付
      await requestPayment(pay);
      // 7 查询后台订单状态
      const res = await request({
        url: "my/orders/chkOrder",
        methods: "POST",
        header,
        data: {
          order_number
        }
      });
      await showToast({
        title: "支付成功"
      })
      // 8 手动删除缓存中已经支付了的商品
      let newCart = wx.getStorageSync("cart");
      newCart = newCart.filter(v => !v.checked);
      wx.setStorageSync("cart", newCart);
      // 9 支付成功后跳转到订单页面
      wx.navigateTo({
        url: "/pages/order/index"
      })
    } catch (error) {
      await showToast({
        title: "支付失败"
      })
      console.log(error);
    } */
  }
})